"""
Module to handle spritesheets.
"""

#  Copyright 2010 Nick Ryder.

#  This file is part of Minigame Mayhem.

#  Minigame Mayhem is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  Minigame Mayhem is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with Minigame Mayhem.  If not, see <http://www.gnu.org/licenses/>.

import pygame

class SpriteSheetCreator:
   "Class to create spritesheets."

   def __init__(self):
      self.surface = None

   def create_empty(self, spritelist, spritecolour=(255, 255, 255), 
      bordercolour=(0, 0, 0)):
      "Create an empty spritesheet containing borders."
      # Find total width and height of spritesheet
      width = 1
      height = 1
      for row in spritelist:
         rowheight = row[0][1]
         rowwidth = 1
         sameheight = True
         for rect in row:
            if rect[1] != rowheight:
               sameheight = False
            rowwidth += rect[0] + 1
         if rowwidth > width:
            width = rowwidth
         if sameheight:
            height += rowheight + 1
         else:
            print "All sprites in a row must have the same height"
            return
      print "Create surface (%d x %d)" % (width, height)
      self.surface = pygame.Surface((width, height))      
      self.surface.fill(spritecolour)
      # Fill surface with colours
      position = [0, 0]
      for row in spritelist:
         for x in range(self.surface.get_width()):
            position[0] = x
            self.surface.set_at(position, bordercolour)
         position[0] = 0
         for y in range(row[0][1]):
            position[1] += 1
            self.surface.set_at(position, bordercolour)
            for rect in row:
               position[0] += rect[0] + 1
               self.surface.set_at(position, bordercolour)
            position[0] = 0
         position[1] += 1
      for x in range(self.surface.get_width()):
         position[0] = x
         self.surface.set_at(position, bordercolour)
            
   def save(self, filename):
      "Save image"
      if self.surface != None:
         print "saving image (%d x %d)" % (self.surface.get_width(), self.surface.get_height())
         pygame.image.save(self.surface, filename)

class SpriteSheet:
   "Sprite sheet class."

   def __init__(self, filename = None):
      self.filename = None
      self.image = None
      if filename != None:
         self.set_filename(filename)
         self.load_image()

   def load_image(self):
      "Load spritesheet image from file."
      if self.filename != None:
         self.image = pygame.image.load(self.filename).convert()

   def set_filename(self, filename):
      "Set name of spritesheet file"
      self.filename = filename

   def get_image(self, rect, colourkey = None):
      "Get image from spritesheet."
      if self.image != None:
         image = pygame.Surface(rect.size)
         image.blit(self.image, (0, 0), rect)
         if colourkey == -1:
            colourkey = image.get_at((0, 0))
            image.set_colorkey(colourkey)
         return image
      else:
         print "must load image before getting images."
         return None

   def get_images(self, rects):
      "Get list of images from spritesheet."
      if self.image != None:
         images = []
         for rect in rects:
            images.append(self.get_image(rect))
         return images
      else:
         print "must load image before getting images."
         return None

if __name__ == "__main__":
      sc = SpriteSheetCreator()
      size1 = (50, 50)
      rects = []
      rects.append([size1, size1])
      rects.append([size1, size1])
      rects.append([size1, size1])
      sc.create_empty(rects)
      sc.save("mazasprites.bmp")
